<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	set_html('title','申请成为配送员');
	$member=$this->pdo_get('member',array('mid'=>$_SESSION['uid']));
	$return['applystatus']=$this->getcolumn('apply',array('mid'=>$_SESSION['uid']),'status');
	if($return['applystatus']==1){
		//pdo_delete('jy_peisong_apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$apply=$this->get('apply',array('mid'=>$_SESSION['uid']),array('id','remark'));
	if($apply['id']){
		$return['apply']=$apply;
		$return['applystatus']=$this->getcolumn('apply',array('mid'=>$_SESSION['uid']),'status');
	}
	//判断是否满足申请分销商条件
	$can_apply=true;
	$return['can_apply']=$can_apply;	
	$return['member']=$member;
	$return['post']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['post']['diyfields']=$this->setting['diyformfields']?:array();
	$groups=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid']),'','id');
	$return['groups'][0]=array('id'=>0,'title'=>'无配送点');
	foreach($groups as $k=>$item){
		$return['groups'][]=$item;
		if($item['id']==$return['post']['group_id']){
			$return['post']['groupindex']=$k;
		}
	}
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if($_SESSION['container']!='wxapp' && $return['pluginset']['is_smscode'] && !check_sms_code($post['smscode'])){
		exi('手机验证码错误！','error');
	}
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if(!$post['group_id']){
		//exi('请输入申请配送点！','error');
	}
	$isadd=$this->get('member',array('mobile'=>$post['mobile'],'uniacid'=>$_SESSION['uniacid']),array('id'));
	if($isadd['id']){
		exi('该手机号已申请过！','error');
	}
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'diyformdata'=>ijson_encode($post['diyfields']),
	);
	if($post['wechat']){
		$member_update['wechat']=trim($post['wechat']);
	}
	model('core/member')->member_update($_SESSION['uid'],$member_update);
	$data=array(
		'title'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'group_id'=>intval($post['group_id']),
		'desc'=>trim($post['desc']),
		'diyformdata'=>ijson_encode($post['diyfields']),
		'status'=>0,
	);
	$pid=$this->model('member')->register($data);
	$this->model('member')->apply_agent($pid);
	exi('申请成功！请耐心等待管理员审核！','','jy_peisong/index/');
}